'use strict';

const Controller = require('egg').Controller;

class FileController extends Controller {
    async edit() {
        const { ctx, app } = this
        const { type, title, content, order } = ctx.request.body
        ctx.validate({
            type: {
                type: 'string',
                required: true,
                desc: '类型'
            },
            title: {
                type: 'string',
                required: true,
                desc: '标题'
            },
            content: {
                type: 'string',
                required: true,
                desc: '内容'
            },
            order: {
                type: 'int',
                desc: '排序'
            }
        })
        let list = await app.model.File.create({ type, title, content, order })
        ctx.apiSuccess(list)
    }
    //列表
    async list(){
        const {ctx,app}=this
        const {type,title}=ctx.query
        ctx.validate({
            type:{
                type:'string',
                required:true,
                desc:'类型'
            }
        })
        let where={
            type
        },Op=app.Sequelize.Op
        if(title!=''&&title!=undefined){
            where={
                title:{
                    [Op.like]:'%'+title+'%'
                }
            }
        }
        let list=await ctx.page(app.model.File,where)
        ctx.apiSuccess(list)
    }
    //删除
    async del(){
        const {ctx,app}=this
        const id=ctx.params.id
        let list=await app.model.File.findByPk(id)
        if(!list){
            ctx.throw(400,'数据不存在')
        }
        list.destroy()
        ctx.apiSuccess('删除成功')
    }
}

module.exports = FileController;
